package com.huishao.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huishao.demo.entity.vo.PageToDailyForm;
import com.huishao.demo.mapper.DailyFormMapper;
import com.huishao.demo.entity.DailyForm;
import com.huishao.demo.service.DailyFormService;
import org.springframework.stereotype.Service;
import com.huishao.demo.common.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huishao.demo.utils.MybatisPlusUtil;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * (DailyForm)表服务实现类
 *
 * @author huishao
 * @since 2023-02-17 13:55:40
 */
@Service("dailyFormService")
public class DailyFormServiceImpl extends ServiceImpl<DailyFormMapper, DailyForm> implements DailyFormService {

    final String SUCCESS_STATUS = "正常";
    final String ERROR_STATUS = "异常";


    @Override
    public PageResult getPage(Integer currentPage, Integer size, DailyForm dailyForm) {
        Page<DailyForm> page = new Page<>(currentPage, size);
        QueryWrapper<DailyForm> queryWrapper = MybatisPlusUtil.queryWrapperBuilder(dailyForm);
        queryWrapper.orderByDesc("create_time");
        this.page(page, queryWrapper);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        List<PageToDailyForm> mylist = new ArrayList<>();
        for(DailyForm daily : page.getRecords()){
            PageToDailyForm pageToDailyForm = new PageToDailyForm();
            pageToDailyForm.setId(daily.getId());
            pageToDailyForm.setCreateTime(simpleDateFormat.format(daily.getCreateTime()));
            pageToDailyForm.setPatrol(checkStatus(daily.getValue(), "Patrol")?ERROR_STATUS:SUCCESS_STATUS);
            pageToDailyForm.setServer(checkStatus(daily.getValue(), "Server")?ERROR_STATUS:SUCCESS_STATUS);
            pageToDailyForm.setIp(daily.getWriterFrom()!=null?daily.getWriterFrom():"过时数据！暂无来源！");
            mylist.add(pageToDailyForm);
        }
        return new PageResult((int) page.getTotal(), mylist);
    }

    private Boolean checkStatus(String value,String flag) {
        // 对 value 进行字符串查询 ，如果有个 no 存在 ，视为有问题
        String[] testStr = value.split("Server");
        if("Server".equals(flag)){
            return testStr[1].contains("no");
        }else {
            return testStr[0].contains("no");
        }

    }


}

